﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BusinessLayer;
using BusinessLayer.WorldArtPrice;
using BusinessLayer.WorldArtPrice.Entities;
using BusinessLayer.WorldArtPrice.Models;
using CommandLib;
using WorldArtPrice.Models;

namespace WorldArtPrice.Controllers
{
    public class PriceController : Controller
    {
        public ActionResult Index(string keyWord, DateTime? fromDate, DateTime? toDate, int? country, int? category, int? pageIndex)
        {
            PriceSearchResultModel pricesSearchResultsModel = new PriceSearchResultModel();
            pricesSearchResultsModel.KeyWord = Server.UrlDecode(keyWord == null ? string.Empty : keyWord);
            pricesSearchResultsModel.Title1 = Server.UrlDecode(keyWord == null ? string.Empty : keyWord);
            pricesSearchResultsModel.FromDate = fromDate;
            pricesSearchResultsModel.ToDate = toDate;
            pricesSearchResultsModel.CountryId = Convert.ToInt32(country == null ? -1 :country);
            pricesSearchResultsModel.ItemTypeId = Convert.ToInt32(category == null ? -1 :category);
            pricesSearchResultsModel.PageIndex = Convert.ToInt32(pageIndex == null ? 1 : pageIndex);
            pricesSearchResultsModel.ListPrices = new PriceBL().Search(Server.UrlDecode(keyWord == null ? string.Empty : keyWord),
                                                                        (fromDate == null ? new DateTime(1900, 1, 1) : Convert.ToDateTime(fromDate)),
                                                                        (toDate == null ? new DateTime(1900, 1, 1) : Convert.ToDateTime(toDate)),
                                                                        Convert.ToInt32(country == null ? -1 : country),
                                                                        Convert.ToInt32(category == null ? -1 : category),
                                                                        Convert.ToInt32(pageIndex == null ? 1 : pageIndex), 30);
            if ((pageIndex == null ? 1 : pageIndex) == 1 || Session[Constant.SESSION_TOTAL_RECORD_PRICES_SEARCH] == null)
            {
                var totalRecord = 0;
                new PriceBL().GetCountByKeyWord(Server.UrlDecode(keyWord == null ? string.Empty : keyWord),
                                                (fromDate == null ? new DateTime(1900, 1, 1) : Convert.ToDateTime(fromDate)),
                                                (toDate == null ? new DateTime(1900, 1, 1) : Convert.ToDateTime(toDate)),
                                                Convert.ToInt32(country == null ? -1 : country),
                                                Convert.ToInt32(category == null ? -1 : category),
                                                 out totalRecord);
                Session[Constant.SESSION_TOTAL_RECORD_PRICES_SEARCH] = totalRecord;
            }
            pricesSearchResultsModel.TotalRecord = Convert.ToInt32(Session[Constant.SESSION_TOTAL_RECORD_PRICES_SEARCH] != null ? Session[Constant.SESSION_TOTAL_RECORD_PRICES_SEARCH] : 0);
            
            List<Countrys> listCoutry = new CountryBL().GetAll();
            listCoutry.Insert(0, new Countrys { Id = -1, Country = "All countries ----------" });
            pricesSearchResultsModel.ListCoutry = listCoutry;

            List<ItemType> listItemType = new ItemTypeBL().GetAll();
            listItemType.Insert(0, new ItemType { Id = -1, Name = "All category ----------" });
            pricesSearchResultsModel.ListItemType = listItemType;

            return View(pricesSearchResultsModel);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Index(PriceSearchResultModel pricesSearchResultsModel)
        {
            if (pricesSearchResultsModel.FromDate != null)
            {
                try
                {
                    DateTime auctionTime = Convert.ToDateTime(pricesSearchResultsModel.FromDate);                    
                }
                catch
                {
                    ModelState.AddModelError("AuctionTime", "AuctionTime wrong format");
                }
            }
            if (pricesSearchResultsModel.ToDate != null)
            {
                try
                {
                    DateTime auctionTime = Convert.ToDateTime(pricesSearchResultsModel.ToDate);
                }
                catch
                {
                    ModelState.AddModelError("AuctionTime", "AuctionTime wrong format");
                }
            }
            if (pricesSearchResultsModel.FromDate == null && pricesSearchResultsModel.CountryId == -1 && pricesSearchResultsModel.ItemTypeId == -1 && string.IsNullOrWhiteSpace(pricesSearchResultsModel.Title1) && string.IsNullOrWhiteSpace(pricesSearchResultsModel.Title2))
            {                
                ModelState.AddModelError("Title", "Name must be min. one letter");                               
            }
            if (ModelState.IsValid == false)
            {
                pricesSearchResultsModel.ListPrices = new PriceBL().Search(string.Empty, new DateTime(1900, 1, 1), new DateTime(1900, 1, 1), -1, -1, 1, 30);
                if ((pricesSearchResultsModel.PageIndex == 0 ? 1 : pricesSearchResultsModel.PageIndex) == 1 || Session[Constant.SESSION_TOTAL_RECORD_PRICES_SEARCH] == null)
                {
                    var totalRecord = 0;
                    new PriceBL().GetCountByKeyWord(string.Empty, new DateTime(1900, 1, 1), new DateTime(1900, 1, 1), -1, -1, out totalRecord);
                    Session[Constant.SESSION_TOTAL_RECORD_PRICES_SEARCH] = totalRecord;
                }
                pricesSearchResultsModel.TotalRecord = Convert.ToInt32(Session[Constant.SESSION_TOTAL_RECORD_PRICES_SEARCH] != null ? Session[Constant.SESSION_TOTAL_RECORD_PRICES_SEARCH] : 0);

                List<Countrys> listCoutry = new CountryBL().GetAll();
                listCoutry.Insert(0, new Countrys { Id = -1, Country = "All countries ----------" });
                pricesSearchResultsModel.ListCoutry = listCoutry;

                List<ItemType> listItemType = new ItemTypeBL().GetAll();
                listItemType.Insert(0, new ItemType { Id = -1, Name = "All category ----------" });
                pricesSearchResultsModel.ListItemType = listItemType;
            
                return View(pricesSearchResultsModel);
            }
            else
            {
                return RedirectToAction("Index", "Price", 
                    new { keyword = Server.UrlEncode(string.IsNullOrEmpty(pricesSearchResultsModel.Title1) ? pricesSearchResultsModel.Title2 : pricesSearchResultsModel.Title1),
                          fromDate=pricesSearchResultsModel.FromDate,
                          toDate = pricesSearchResultsModel.ToDate,
                          country = pricesSearchResultsModel.CountryId,
                          category = pricesSearchResultsModel.ItemTypeId
                        , pageIndex = 1 });
            }
        }

        public ActionResult GetByArtist(int artistsId, int? pageIndex)
        {
            PriceArtistModel pricesArtistsModel = new PriceArtistModel();
            pricesArtistsModel.Artist = new ArtistBL().GetArtistById(artistsId);
            pricesArtistsModel.PageIndex = Convert.ToInt32(pageIndex == null ? 1 : pageIndex);

            pricesArtistsModel.ListPrices = new PriceBL().GetByArtistId(artistsId, Convert.ToInt32(pageIndex == null ? 1 : pageIndex), 30);
            if ((pageIndex == null ? 1 : pageIndex) == 1 || Session[Constant.SESSION_TOTAL_RECORD_PRICES_ARTISTS] == null)
            {
                var totalRecord = 0;
                new PriceBL().GetCountByArtistId(artistsId, out totalRecord);
                Session[Constant.SESSION_TOTAL_RECORD_PRICES_ARTISTS] = totalRecord;
            }
            pricesArtistsModel.TotalRecord = Convert.ToInt32(Session[Constant.SESSION_TOTAL_RECORD_PRICES_ARTISTS] != null ? Session[Constant.SESSION_TOTAL_RECORD_PRICES_ARTISTS] : 0);

            List<Countrys> listCoutry = new CountryBL().GetAll();
            listCoutry.Insert(0, new Countrys { Id = -1, Country = "All countries ----------" });
            pricesArtistsModel.ListCoutry = listCoutry;

            List<ItemType> listItemType = new ItemTypeBL().GetAll();
            listItemType.Insert(0, new ItemType { Id = -1, Name = "All category ----------" });
            pricesArtistsModel.ListItemType = listItemType;

            return View(pricesArtistsModel);
        }

        public ActionResult Detail(int priceId)
        {
            PricesDetailModel priceDetailModel = new PricesDetailModel();
            Prices price = new PriceBL().GetById(priceId);
            priceDetailModel.Artist = new ArtistBL().GetArtistById(price.ArtistId);
            priceDetailModel.Price = new PriceBL().GetById(priceId);
            priceDetailModel.EstimatedCurrency = new CurrencyBL().GetById(price.EstimatedCurrency);
            priceDetailModel.PriceCurrency = new CurrencyBL().GetById(price.PriceCurrency);
            priceDetailModel.ListArtWork = new ArtWorkBL().GetByPriceId(price.ID);
            priceDetailModel.AuctionHouse = new AuctionHouseBL().GetById(price.AuctionHouse);
            priceDetailModel.AuctionHouseSublocation = new AuctionHouseSublocationBL().GetById(price.AuctionHouseSublocation);
            priceDetailModel.ItemType = new ItemTypeBL().GetById(price.ItemType);
            return View(priceDetailModel);
        }

    }
}
